package com.member.server.domain.dto;

import com.alibaba.fastjson.JSON;
import com.member.server.entity.*;
import com.member.server.enums.RecordStatus;
import com.member.server.enums.RecordType;

import java.util.Collections;

/**
 * @author gaojx1993
 * @version 1.0
 * @description AccRechargeDTO
 * @since 2023/7/11 22:21
 */
public class AccRechargeDTO extends AccOpDTO {

    private final Long rechargeAmount;

    private final Account account;

    private final ChannelConfig channelConfig;

    private RecordAccountRecharge recordAccountRecharge;

    public AccRechargeDTO(Member member, Account account, Long rechargeAmount, ChannelConfig channelConfig) {
        super(member);
        this.rechargeAmount = rechargeAmount;
        this.account = account;
        this.channelConfig = channelConfig;
        init();
    }

    @Override
    public void init() {
        // 生成充值流水
        genRecord();
        // 生成充值流水详情
        genRecordAccountRecharge();
        // 生成账户变动记录
        genAccountLogs();
    }

    @Override
    public void genRecord() {
        record = new Record();
        record.setGmtCreate(gmtCreate);
        record.setGmtUpdate(gmtCreate);
        record.setMemberId(member.getId());
        record.setMemberName(member.getName());
        record.setType(RecordType.recharge.getCode());
        record.setStatus(RecordStatus.finished.getCode());
        record.setTotalAmount(rechargeAmount);
        record.setRealAmount(rechargeAmount);
        record.setGmtFinish(gmtCreate);
        record.setRemark(recordRemark);
    }

    @Override
    public void genAccountLogs() {
        AccountLog accountLog = new AccountLog();
        accountLog.setGmtCreate(gmtCreate);
        accountLog.setGmtUpdate(gmtCreate);
        accountLog.setAccountId(account.getId());
        accountLog.setBeforeBalance(account.getBalance());
        accountLog.setAfterBalance(account.getBalance() + rechargeAmount);
        accountLogs = Collections.singletonList(accountLog);
    }

    public Long getRechargeAmount() {
        return rechargeAmount;
    }

    public Account getAccount() {
        return account;
    }

    public ChannelConfig getChannelConfig() {
        return channelConfig;
    }

    public RecordAccountRecharge getRecordAccountRecharge() {
        return recordAccountRecharge;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    private void genRecordAccountRecharge() {
        recordAccountRecharge = new RecordAccountRecharge();
        recordAccountRecharge.setGmtCreate(gmtCreate);
        recordAccountRecharge.setGmtUpdate(gmtCreate);
        recordAccountRecharge.setAccountId(account.getId());
        recordAccountRecharge.setStatus(1);
        recordAccountRecharge.setPayAmount(rechargeAmount);
        recordAccountRecharge.setArrivedAmount(rechargeAmount);
        recordAccountRecharge.setChannelId(channelConfig.getId());
        recordAccountRecharge.setChannelName(channelConfig.getName());
    }
}
